package com.framework.component.extension.register;

import com.framework.component.extension.anno.Extension;
import com.framework.component.extension.anno.ExtensionPoint;
import com.framework.component.extension.context.BizScenario;
import com.framework.component.extension.context.ExtensionContext;
import org.springframework.stereotype.Component;

/**
 * 扩展点执行器
 */
@Component
public class ExtensionExecutor {

    private final ExtensionRepository extensionRepository;

    public ExtensionExecutor(ExtensionRepository extensionRepository) {
        this.extensionRepository = extensionRepository;
    }

    public void execute(Class<? extends ExtensionPoint> extensionPointClass, ExtensionContext context) {
        Extension extensionAnnotation = extensionPointClass.getAnnotation(Extension.class);
        if (extensionAnnotation == null) {
            return;
        }
        BizScenario bizScenario = BizScenario.valueOf(extensionAnnotation.bizId(), extensionAnnotation.useCase(), extensionAnnotation.scenario());
        ExtensionPoint extensionPoint = extensionRepository.getRepositoryRepo().get(calculateExtensionPoint(extensionPointClass, bizScenario.getUniqueKey()));
        extensionPoint.execute(context);
    }

    private String calculateExtensionPoint(Class<? extends ExtensionPoint> targetClz, String bizUniqueKey) {
        return bizUniqueKey + "." + targetClz.getName();
    }
}
